package net.fkm.gaodemaptest.utils;

import com.amap.api.maps.model.LatLng;

public class AmapUtil {
    //测量两点的距离
    public static long measureDistance(LatLng fromPoint, LatLng toPoint) {
        double EARTH_RADIUS = 6378137;
        long distance = 0;
        double startLongitude = fromPoint.longitude;
        double startLatitude = fromPoint.latitude;
        double endLongitude = toPoint.longitude;
        double endLatitude = toPoint.latitude;
        double radLatitude1 = startLatitude * Math.PI / 180.0;
        double radLatitude2 = endLatitude * Math.PI / 180.0;
        double a = Math.abs(radLatitude1 - radLatitude2);
        double b = Math.abs(startLongitude * Math.PI / 180.0 - endLongitude * Math.PI / 180.0);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLatitude1) * Math.cos(radLatitude2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        distance = Math.round(s * 10000) / 10000; // 返回距离单位是米
        return distance;
    }
}
